<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
  <link rel="stylesheet" href="css/base.css">
  <script src="https://s4.ssl.qhres.com/!928fb688/spritejs.min.js"></script>
  <style>
    html, body {
      width: 100%;
      height: 100%;
      padding: 0;
      margin: 0;
      overflow: hidden;
    }

    #scene-container {
      width: 100%;
      height: 100%;
    }
    #box-rect-demo {
      position: absolute;
      padding: 20px 0 0 20px;
      top: 0;
    }
  </style>
</head>
<body>
  <div id="scene-container"></div>
  <script>
  const {Scene, Sprite} = spritejs
  const scene = new Scene('#scene-container', {
    viewport: ['auto', 'auto'],
    resolution: [1200, 1600],
    stickMode: 'height',
  })
  const cacheMap = {}
  class Block extends Sprite {
    get cache() {
      return cacheMap[this.attr('bgcolor')]
    }
    set cache(cacheContext) {
      if(cacheContext) {
        cacheMap[this.attr('bgcolor')] = cacheContext
      }
    }
  }

  function randomColor() {
    return ['red', 'blue', 'green', 'cyan', 'black'][Math.floor(Math.random() * 5)]
  }

  const layer = scene.layer()
  for(let i = 0; i <= 1000; i++) {
    const b = new Block()
    b.attr({
      pos: [100 + Math.random() * 1000, 100 + Math.random() * 1400],
      size: [25, 25],
      rotate: 360 * Math.random(),
      bgcolor: randomColor(),
    })
    setInterval(() => {
      b.attr({
        bgcolor: randomColor(),
      })
    }, 100)
    layer.append(b)
  }

  </script>
</body>
</html>
